package com.example.demo.controller;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

import static com.baomidou.mybatisplus.extension.toolkit.Db.save;



//文件上传


@RestController
public class FileController{
    private static final String UPLOADED_FOLDER=System.getProperty("user. dir")+"/upload/";

    //上传请求
    @PostMapping("/up")
    public String upload(String nickname, MultipartFile f ,HttpServletRequest request) throws IOException {
        System.out.println(nickname);
        System.out.println("文件大小："+f.getSize());
        System. out. println(f.getContentType());
        System. out. println(f.getOriginalFilename());


        String Path=request.getServletContext().getRealPath("/up");
 //       System.out. println(System. getProperty("user.dir"));
        System.out.println(Path);

        saveFile(f,Path);
        return "上传成功";
    }

    //保存文件函数
    public void saveFile(MultipartFile f,String Path) throws IOException {
        File upDir=new File(Path);
        if(!upDir.exists()){
            upDir. mkdir();
        }
        File file =new File(Path+f.getOriginalFilename());
        f.transferTo(file);
    }
}


